/**
 * @Title: DataUtils.java
 * @Package: com.jason.fingerprint.utils
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月24日 下午8:12:01
 * @Version: V1.0
 */
package net.hzlj.platform.enterprise.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import net.hzlj.platform.enterprise.BootApp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @ClassName: DataUtils
 * @Description: 时间格式化工具类
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月24日 下午8:12:01
 */
@SuppressLint({ "UseValueOf", "SimpleDateFormat" })
public class DateUtils {

	public static final String DATE_YMD_HMS = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_YMD_HMS_SHORT = "yyyyMMddHHmmss";

	public static final String DATE_YMD_HM = "yyyy-MM-dd HH:mm";

	public static final String DATE_YMD_HM_SHORT = "yyyyMMddHHmm";

	public static final String DATE_YMD_H = "yyyy-MM-dd HH";

	public static final String DATE_YMD_H_SHORT = "yyyyMMddHH";

	public static final String DATE_YMD = "yyyy-MM-dd";

	public static final String DATE_HMS = "HH:mm:ss";

	public static final String DATE_YMD_SHORT = "yyyyMMdd";

	public static final String DATE_YM = "yyyy-MM";

	public static final String DATE_YM_SHORT = "yyyyMM";

	public static Calendar getCurrentCalendar(String d){
		return getCurrentCalendar(d,DATE_YMD_HMS);
	}

	public static Calendar getCurrentCalendar(String d,String format){
		Calendar calendar = new GregorianCalendar();
		if (TextUtils.isEmpty(d)){
			return calendar;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date  = simpleDateFormat.parse(d);
			calendar.setTime(date);
			return calendar;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return calendar;
	}


	/***
	 * 当前时间所处当月的第几天
	 * @return
	 */
	public static int getCurrentDay(){
		Calendar calendar = new GregorianCalendar();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}


	/***
	 * 计算相差天数
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String smdate, String bdate){
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_YMD);
		Calendar cal = Calendar.getInstance();
		long time1,time2 = 0l;
		try {
			cal.setTime(sdf.parse(smdate));
			time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			time2 = cal.getTimeInMillis();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}


	/***
	 *
	 * @Descripton: 获取当前时间所在天的开始时间
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static Date getCurrentDayBeginDate(){
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/***
	 *
	 * @Descripton: 获取当前时间所在天的结束时间
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static Date getCurrentDayEndDate(){
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/***
	 *
	 * @Descripton: 获取当前时间所在天的开始周
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static Date getCurrentWeekBeginDate(){
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}

	/***
	 *
	 * @Descripton: 获取当前时间所在天的结束周
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static Date getCurrentWeekEndDate(){
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return calendar.getTime();
	}

	/***
	 *
	 * @Descripton: long转String eg:1415547962595 --> 2014-11-09 23:46:02
	 * @Param @param time 格式化的时间eg:1415547962595
	 * @Param @param dateFormat 格式化样式 eg:yyyy-MM-dd HH:mm:ss
	 * @Param @return 格式化之后的字符串 eg:2014-11-09 23:46:02
	 * @Return $
	 * @Throws
	 */
	public static String convertDateLongToString(String time,String dateFormat){
		if (StringUtils.isEmpty(time) || StringUtils.isEmpty(dateFormat)) {
			return null;
		}
		Date date = new Date(Long.parseLong(time));
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		return formatter.format(date);
	}

	public static String convertDateLongToString(long time,String dateFormat){
		if (time == 0l || StringUtils.isEmpty(dateFormat)) {
			return null;
		}
		Date date = new Date(time);
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		return formatter.format(date);
	}

	/**
     * 字符串转化成日期
     *
     * @param dateStrVal 日期字符串
     * @param dateFormat 日期类型
     * @return 日期
     */
    public static Date convertStrToDate(String dateStrVal, String dateFormat) {
        if (StringUtils.isEmpty(dateStrVal) || StringUtils.isEmpty(dateFormat))
            return null;

        Date dateVal = null;

        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
        	dateVal = formatter.parse(dateStrVal);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateVal;
    }


    /**
     * 字符串转化成long类型时间
     *
     * @param dateStrVal 日期字符串
     * @param dateFormat 日期类型
     * @return 日期
     */
    public static Long convertStrToLong(String dateStrVal, String dateFormat) {
        if (StringUtils.isEmpty(dateStrVal) || StringUtils.isEmpty(dateFormat))
            return 0l;

        Date dateVal = convertStrToDate(dateStrVal, dateFormat);

        if (dateVal == null)
            return 0l;

        return new Long(dateVal.getTime());
    }

	public static long getTelephoneReportingTime(){

		long networkTime = AppPreferencesUtils.getLong(BootApp.getAppContext(),PreferencesUtils.KEY_NETWORK_TIME,0l);
		long nowTime = Calendar.getInstance().getTimeInMillis() / 1000;
		long diff = nowTime - networkTime;
		if (diff < 60 * 60){
			return nowTime;
		}
		return networkTime == 0L ? nowTime : networkTime ;
	}

}
